<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <my-grandfather></my-grandfather>
    </div>

    <!-- 爷爷组件 -->
    <template id="grandfather">
        <div>
            <h1>
                我是爷爷组件 -- {{ count }}
            </h1>
            <button @click="send">传递</button>
            <my-father></my-father>
        </div>
    </template>


    <!-- 父组件 -->
    <template id="father">
        <div>
            <h3>
                我是父组件
            </h3>
            <my-son></my-son>
        </div>
    </template>


    <!-- 子组件 -->
    <template id="son">
        <div>
            <h5>
                我是子组件 -- {{ count }}
            </h5>
        </div>
    </template>


    <script src="./vue.js"></script>
    <script>

        // 事件总线
        const bus = new Vue()

        // 子组件 
        const Son = {
            template: "#son",
            data() {
                return {
                    count: null
                }
            },
            mounted() {
                // bus.$on(事件名称, )
                bus.$on("send", (data) => {
                    this.count = data
                })
            },
            beforeDestroy() {
                // 清空，因为$on是挂起的状态
                bus.$off("send")
            }
        }

        // 父组件 
        const Father = {
            template: "#father",
            components: {
                "my-son": Son
            }
        }


        // 爷爷组件 
        const GrandFather = {
            template: "#grandfather",
            components: {
                "my-father": Father
            },
            data() {
                return {
                    count: 888
                }
            },
            methods: {
                send() {
                    // bus.$emit(事件名称, 传递的数据)
                    bus.$emit("send", this.count)
                }
            }
        }

        new Vue({
            el: "#app",
            components: {
                "my-grandfather": GrandFather
            }
        })
    </script>
</body>

</html>